//
//  TaoBaoViewController.m
//  Camera
//
//  Created by gml on 2018/10/17.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "TaoBaoViewController.h"

#import "HeaderBgView.h"
#import "OrderTableViewCell.h"
#import "Order2TableViewCell.h"

#import "MJRefresh.h"

@interface TaoBaoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) HeaderBgView *headView;
@property (nonatomic,strong) NSArray *list;

@end
static NSString *OderCellID = @"OrderTableViewCell";
static NSString *Order2TableViewCellID = @"Order2TableViewCell";
@implementation TaoBaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    CAGradientLayer *colorLayer = [CAGradientLayer layer];
    colorLayer.colors = @[(__bridge id)[UIColor orangeColor].CGColor,(__bridge id)[UIColor redColor].CGColor];
    colorLayer.startPoint = CGPointMake(0.5, 0);
    colorLayer.endPoint = CGPointMake(1, 0);
    colorLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, 500);
    [self.view.layer addSublayer:colorLayer];
    [self.view addSubview:self.tableview];
    self.navigationController.navigationBar.alpha = 0;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    OrderTableViewCell *orderCell = [tableView dequeueReusableCellWithIdentifier:OderCellID];
    Order2TableViewCell *order2Cell = [tableView dequeueReusableCellWithIdentifier:Order2TableViewCellID];
    NSString *cellClass = _list[indexPath.row];
    if ([cellClass isEqualToString:NSStringFromClass([OrderTableViewCell class])]) {
        
        return orderCell;
    }else if ([cellClass isEqualToString:NSStringFromClass([Order2TableViewCell class])]){
        return order2Cell;
    }
    else{
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
      NSString *cellClass = _list[indexPath.row];
    if ([cellClass isEqualToString:NSStringFromClass([OrderTableViewCell class])]) {
        
        return 100.0;
    }else if ([cellClass isEqualToString:NSStringFromClass([Order2TableViewCell class])]){
        
        return 250;
    }
    else{
        return 44.0;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    scrollView.scrollEnabled = scrollView.contentOffset.y>=-250;
    NSLog(@"contentOffset.y = %f",(scrollView.contentOffset.y-64)/64);
    self.navigationController.navigationBar.alpha = (scrollView.contentOffset.y-64)/64;
}

-(UITableView *)tableview
{
    if (_tableview == nil) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.origin.y+self.navigationController.navigationBar.bounds.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-self.navigationController.navigationBar.bounds.size.height ) style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        _tableview.estimatedRowHeight = 0;
//        [_tableview setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)];
        [_tableview setTableHeaderView:self.headView];
        _tableview.backgroundColor = [UIColor clearColor];
        [_tableview registerNib:[UINib nibWithNibName:NSStringFromClass([OrderTableViewCell class]) bundle:nil] forCellReuseIdentifier:OderCellID];
        [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [_tableview registerNib:[UINib nibWithNibName:NSStringFromClass([Order2TableViewCell class]) bundle:nil] forCellReuseIdentifier:Order2TableViewCellID];
        [_tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        if (@available(iOS 11.0, *)) {
            _tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        __unsafe_unretained UITableView *tableView = _tableview;
        _tableview.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
            // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 结束刷新
                [tableView.mj_header endRefreshing];
            });
        }];
    }
    return _tableview;
}
- (HeaderBgView *)headView
{
    if (_headView == nil) {
        _headView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([HeaderBgView class]) owner:nil options:nil] lastObject];
        _headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200);
    }
    return _headView;
}
- (NSArray *)list
{
    if (_list == nil) {
        _list = @[NSStringFromClass([OrderTableViewCell class]),NSStringFromClass([Order2TableViewCell class]),NSStringFromClass([Order2TableViewCell class]),NSStringFromClass([UITableViewCell class]),NSStringFromClass([UITableViewCell class]),NSStringFromClass([UITableViewCell class])];
    }
    return _list;
}

@end
